import { defineConfig, loadEnv } from 'vite'
const { resolve } = require('path')
import createVitePlugins from './vite/plugins'
import { viteMockServe } from 'vite-plugin-mock'

// https://vitejs.dev/config/

export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_BASE } = env
  const plugins=createVitePlugins(env, command === 'build')
  plugins.push(viteMockServe({
     mockPath: "mock",// 解析根目录下的mock文件夹
     localEnabled: env.VITE_USE_MOCK === 'true',  // 开发打包开关
     prodEnabled: env.VITE_USE_MOCK === 'false', // 生产打包开关
     supportTs: true, // 打开后，可以读取 ts 文件模块。 请注意，打开后将无法监视.js 文件。
   }));
  
  return {
    base: VITE_APP_BASE,
    plugins: plugins,
    resolve: {
      alias: {
        '~': resolve(__dirname, './'),
        "@": resolve(__dirname, "./src"),
        "components": resolve(__dirname, "./src/components"),
        "styles": resolve(__dirname, "./src/page/styles"),
        "utils": resolve(__dirname, "./src/utils"),
      }
    },
    server: {
      port: 8080,
      proxy: {
        '/api': {
            target: 'http://localhost:8088',
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, '/api')
        },
        // historyApiFallback: {
        //   index: '/index.html' //与output的publicPath
        // },
      }
    },
  }
  
})

